﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic

Namespace Pic
    Public Class BoardObject
        Inherits clsBase
#Region "Private Variables"
        Private _BoardID As Integer
        Private _ObjectID As Integer
        Private _Created As DateTime
        Private _Modified As DateTime
#End Region
#Region "Properties"
        Public Property BoardID() As Integer
            Get
                Return _BoardID
            End Get
            Set(ByVal value As Integer)
                _BoardID = value
            End Set
        End Property
        Public Property ObjectID() As Integer
            Get
                Return _ObjectID
            End Get
            Set(ByVal value As Integer)
                _ObjectID = value
            End Set
        End Property
        Public Property Created() As DateTime
            Get
                Return _Created
            End Get
            Set(ByVal value As DateTime)
                _Created = value
            End Set
        End Property
        Public Property Modified() As DateTime
            Get
                Return _Modified
            End Get
            Set(ByVal value As DateTime)
                _Modified = value
            End Set
        End Property

#End Region
#Region "Constructors"
        Public Sub New(ByVal BoardID_ As Integer, ByVal ObjectID_ As Integer, ByVal Created_ As DateTime, ByVal Modified_ As DateTime)
            Me.BoardID = BoardID_
            Me.ObjectID = ObjectID_
            Me.Created = Created_
            Me.Modified = Modified_
        End Sub
#End Region
#Region "Methods"
        Private Shared Function GetBoardObjectFromReader(ByVal reader_ As IDataReader) As BoardObject
            Dim rtrn As BoardObject = New BoardObject(reader_("Board_Id"), reader_("Object_Id"), reader_("Created"), reader_("Modified"))
            Return rtrn
        End Function

        Public Shared Function GetBoardObjectByEmail(ByVal BoardID_ As Integer, ByVal ObjectID_ As Integer) As BoardObject
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select * from tbl_Board_Object where Board_ID=@BoardID AND Object_ID=@ObjectID"
            Dim reader As IDataReader
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@BoardID", SqlDbType.Int).Value = BoardID_
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                reader = ExecuteReader(cmd, CommandBehavior.SingleRow)
                If reader.Read Then
                    Return GetBoardObjectFromReader(reader)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        Private Shared Function GetBoardObjectListFromReader(ByVal reader As IDataReader) As List(Of BoardObject)
            Dim rtrnList As New List(Of BoardObject)
            Do While reader.Read
                rtrnList.Add(GetBoardObjectFromReader(reader))
            Loop
            Return rtrnList
        End Function
        Public Shared Function GetBoardObjectListByFilter(ByVal Filtre_ As String, ByVal FiltreOrder_ As String) As List(Of BoardObject)
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "select * from website1.dbo.tbl_Board_Object where " & Filtre_ & "' order by " & FiltreOrder_ & " asc"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                Return GetBoardObjectListFromReader(ExecuteReader(cmd, CommandBehavior.Default))
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function InsertBoardObject(ByVal BoardID_ As Integer, ByVal ObjectID_ As Integer) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "insert into tbl_Board_Object(board_id,object_id,created,modified) values(@BoardID,@ObjectID,@created,@modified)"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@BoardID", SqlDbType.Int).Value = BoardID_
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Now.Date
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
 
        Public Shared Function UpdateBoardObject(ByVal BoardID_ As Integer, ByVal ObjectID_ As Boolean) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "update tbl_Board_Object set Modified=@Modified where Board_ID = @Board_ID AND Object_ID=@Object_ID"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Board_ID", SqlDbType.Int).Value = BoardID_
                cmd.Parameters.Add("@Object_ID", SqlDbType.Int).Value = ObjectID_
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function DeleteBoardObject(ByVal BoardID_ As Integer, ByVal ObjectID_ As Integer) As Boolean
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select count(1) from tbl_Board_Object Where Board_ID=@Board_ID AND Object_ID=@Object_ID"
            Dim count As Integer
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.Parameters.Add("@Board_ID", SqlDbType.Int).Value = BoardID_
                cmd.Parameters.Add("@Object_ID", SqlDbType.Int).Value = ObjectID_
                count = ExecuteScalar(cmd)
                If count = 0 Then
                    Return False
                Else
                    cmd.CommandText = "Delete from tbl_Board_Object Where Board_ID=@Board_ID AND Object_ID=@Object_ID"
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@Board_ID", SqlDbType.Int).Value = BoardID_
                    cmd.Parameters.Add("@Object_ID", SqlDbType.Int).Value = ObjectID_
                    Return ExecuteNonQuery(cmd)

                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        'Public Shared Function SilinebilirMi(ByVal ProjeID_ As Integer) As Boolean
        '    Dim conn As New SqlConnection(ConnectionString)
        '    Dim veriString As String = "Select count(1) from tblSondajlar where ProjeID=" & ProjeID_
        '    Dim count As Integer
        '    Try
        '        conn.Open()
        '        Dim cmd As New SqlCommand(veriString, conn)
        '        count = ExecuteScalar(cmd)
        '        If count = 0 Then
        '            Return True
        '        Else
        '            Return False
        '        End If
        '    Catch ex As Exception
        '        Throw ex
        '    Finally
        '        conn.Close()
        '    End Try
        'End Function
#End Region
    End Class
End Namespace

